package cn.uai.activiti8.service.impl;

import cn.uai.activiti8.dto.Result;
import cn.uai.activiti8.entity.User;
import cn.uai.activiti8.mapper.UserMapper;
import cn.uai.activiti8.service.UserService;
import com.alibaba.fastjson2.JSONObject;
import com.github.yitter.idgen.YitIdHelper;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static cn.uai.activiti8.entity.table.UserTableDef.USER;

/**
 * @desc 用户服务类
 * @author uai
 * @since 2025/8/7 11:28
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public String add(String body) {
        User user = JSONObject.parseObject(body, User.class);
        user.setUid(YitIdHelper.nextId());
        user.setCreateTime(LocalDateTime.now());
        userMapper.insert(user);
        return Result.success();
    }

    @Override
    public String update(String uid, String body) {
        return "";
    }

    @Override
    public String delete(String uid) {
        return "";
    }

    @Override
    public User find(Long uid) {
        QueryWrapper queryWrapper = QueryWrapper.create().eq("uid", uid);
        return userMapper.selectOneByQuery(queryWrapper);
    }

    public Long findRidByUid(Long uid) {
        User user = find(uid);
        return user.getRid();
    }

    @Override
    public User userName2uid(String userName) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(USER.UID)
                .where(USER.USERNAME.eq(userName));
        return userMapper.selectOneByQuery(queryWrapper);
    }


}
